"""
人物数据模型（Pydantic schemas）
"""
from typing import Optional, List, Dict, Any
from pydantic import BaseModel, Field, validator
from datetime import datetime

from app.models.character import RoleType, Gender

class CharacterBase(BaseModel):
    """人物基础模型"""
    name: str = Field(..., min_length=1, max_length=100, description="姓名")
    role_type: RoleType = Field(..., description="角色类型")
    gender: Optional[Gender] = Field(None, description="性别")
    age: Optional[int] = Field(None, ge=0, le=150, description="年龄")
    position: Optional[str] = Field(None, max_length=100, description="职位")
    department: Optional[str] = Field(None, max_length=100, description="部门")
    description: Optional[str] = Field(None, description="人物描述和备注")
    tags: Optional[List[str]] = Field(None, description="标签列表")
    basic_info: Optional[Dict[str, Any]] = Field(None, description="基本信息")
    personality_tags: Optional[List[str]] = Field(None, description="性格标签")
    background_info: Optional[Dict[str, Any]] = Field(None, description="背景信息")
    relationship_data: Optional[Dict[str, Any]] = Field(None, description="关系数据")
    meta_data: Optional[Dict[str, Any]] = Field(None, description="扩展元数据")

class CharacterCreate(CharacterBase):
    """创建人物模型"""
    pass

class CharacterUpdate(BaseModel):
    """更新人物模型"""
    name: Optional[str] = Field(None, min_length=1, max_length=100, description="姓名")
    role_type: Optional[RoleType] = Field(None, description="角色类型")
    gender: Optional[Gender] = Field(None, description="性别")
    age: Optional[int] = Field(None, ge=0, le=150, description="年龄")
    position: Optional[str] = Field(None, max_length=100, description="职位")
    department: Optional[str] = Field(None, max_length=100, description="部门")
    description: Optional[str] = Field(None, description="人物描述和备注")
    tags: Optional[List[str]] = Field(None, description="标签列表")
    basic_info: Optional[Dict[str, Any]] = Field(None, description="基本信息")
    personality_tags: Optional[List[str]] = Field(None, description="性格标签")
    background_info: Optional[Dict[str, Any]] = Field(None, description="背景信息")
    relationship_data: Optional[Dict[str, Any]] = Field(None, description="关系数据")
    meta_data: Optional[Dict[str, Any]] = Field(None, description="扩展元数据")

class CharacterBasicInfoUpdate(BaseModel):
    """人物基本信息更新模型"""
    basic_info: Dict[str, Any] = Field(..., description="基本信息")

class PersonalityTagRequest(BaseModel):
    """性格标签请求模型"""
    tag: str = Field(..., min_length=1, max_length=50, description="性格标签")

class CharacterResponse(CharacterBase):
    """人物响应模型"""
    id: int = Field(..., description="主键ID")
    is_active: bool = Field(..., description="是否活跃")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    created_by: str = Field(..., description="创建人")
    updated_by: str = Field(..., description="更新人")
    version: int = Field(..., description="版本号")
    is_deleted: bool = Field(..., description="是否删除")
    
    class Config:
        """配置"""
        from_attributes = True
        json_encoders = {
            datetime: lambda v: v.isoformat()
        }

class CharacterStatistics(BaseModel):
    """人物统计模型"""
    total_characters: int = Field(..., description="总人物数")
    role_distribution: Dict[str, int] = Field(..., description="角色分布")
    department_distribution: Dict[str, int] = Field(..., description="部门分布")
    gender_distribution: Dict[str, int] = Field(..., description="性别分布")